package DoAn;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.rmi.activation.ActivationMonitor;

import javax.swing.*;

public class CapNhatNhanVien extends JFrame implements ActionListener, KeyListener{

	ReadWriteFile rwf = new ReadWriteFile();
	ListNhanVien dsNV = new ListNhanVien();
	NhanVien NV;
	int index = -1;
	
	JButton jbt_CapNhat = new JButton("Cập nhật");
	JButton jbt_Huy = new JButton("Hủy bỏ");
	
	JTextField 	jtf_MaNV = new JTextField(),
				jtf_HoTen = new JTextField(),
				jtf_SoDT = new JTextField(),
				jtf_Email = new JTextField(),
				jtf_NamSinh = new JTextField(),
				jtf_HSL = new JTextField(),
				jtf_HSPC = new JTextField(),
				jtf_ChucVu = new JTextField(),
				jtf_PhongBan = new JTextField();
	JLabel 	jlb_TieuDe = new JLabel("THÔNG TIN NHÂN VIÊN"),
			jlb_MaNV = new JLabel("Mã nhân viên:"),
			jlb_HoTen = new JLabel("Họ và tên:"),
			jlb_GioiTinh = new JLabel("Giới tính:"),
			jlb_SoDT = new JLabel("Số điện thoại:"),
			jlb_Email = new JLabel("Email:"),
			jlb_NamSinh = new JLabel("Năm sinh:"),
			jlb_HSL = new JLabel("Hệ số lương:"),
			jlb_HSPC = new JLabel("Hệ số phụ cấp:"),
			jlb_ChucVu = new JLabel("Chức vụ:"),
			jlb_PhongBan = new JLabel("Phòng ban:");
	JRadioButton jrb_Nam = new JRadioButton("Nam"),
				 jrb_Nu = new JRadioButton("Nữ");
	public void setFont(JLabel jlb){
		jlb.setForeground(Color.BLACK);
		jlb.setFont(new Font("Tahoma", Font.PLAIN, 16));
	}
	
	public void setFont(JTextField jtf){
		jtf.setForeground(Color.BLACK);
		jtf.setFont(new Font("Tahoma", Font.PLAIN, 16));
	}
	
	public void setFont(JButton jbt){
		jbt.setForeground(Color.BLACK);
		jbt.setFont(new Font("Tahoma", Font.BOLD, 16));
	}
	
	public void setFont(JRadioButton jrb){
		jrb.setForeground(Color.BLACK);
		jrb.setFont(new Font("Tahoma", Font.PLAIN, 16));
	}
	
	public void SaveList(){
		NhanVien a = new NhanVien();
		try{ dsNV.List = rwf.getDSNV();}
		catch (Exception e) {dsNV.List = null;}
		//System.out.println("Read file:\n"+dsNV.List[0].GetNV());
		try{
			if(index == -1){
				if(dsNV.isMaNV(jtf_MaNV.getText()))
					a.MaNV = jtf_MaNV.getText();
				else jtf_MaNV.setBackground(Color.CYAN);
			}
			else a.MaNV = jtf_MaNV.getText();		
			a.HoTen = jtf_HoTen.getText();
			if(jrb_Nam.isSelected()) a.GioiTinh = true;
			else a.GioiTinh = false;			
			try{
				int i = Integer.parseInt(jtf_SoDT.getText());
				a.SoDT = jtf_SoDT.getText();
			}catch (Exception e) {
				jtf_SoDT.setBackground(Color.CYAN);
			}
			a.Email = jtf_Email.getText();
			try{
				a.NamSinh = Integer.parseInt(jtf_NamSinh.getText());
			}catch (Exception e) { jtf_NamSinh.setBackground(Color.CYAN);}			
			try{
				a.HeSoLuong = Float.parseFloat(jtf_HSL.getText());
			}catch (Exception e) { jtf_HSL.setBackground(Color.CYAN);}			
			try{
				a.HeSoPhuCap = Float.parseFloat(jtf_HSPC.getText());
			}catch (Exception e) { jtf_HSPC.setBackground(Color.CYAN);}
			a.ChucVu = jtf_ChucVu.getText();
			a.PhongBan = jtf_PhongBan.getText();
			if(index == -1){
				a = new NhanVien(a);				
				dsNV.List = dsNV.Add(a);				
			}else {				
				dsNV.List[index] = a;
			}
			//rwf.Save(dsNV.List);
		}catch (Exception e) {
			for(int i = 0; i < dsNV.List.length; i++){
				System.out.println(dsNV.List[i].GetNV());
			}
			JOptionPane.showMessageDialog(this, "Lỗi: " + e.getMessage(), "Lỗi",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	public void SetLayOut(){
		jlb_TieuDe.setForeground(Color.BLACK);
		jlb_TieuDe.setFont(new Font("Tahoma", Font.BOLD, 20));
		jlb_TieuDe.setBounds(new Rectangle(65,10,300,25));
		
		setFont(jlb_MaNV);setFont(jlb_HoTen);setFont(jlb_GioiTinh);setFont(jlb_SoDT);setFont(jlb_NamSinh);
		setFont(jlb_Email);setFont(jlb_HSL);setFont(jlb_HSPC);setFont(jlb_ChucVu);setFont(jlb_PhongBan);		
		setFont(jtf_MaNV);setFont(jtf_HoTen);setFont(jtf_SoDT);setFont(jtf_NamSinh);
		setFont(jtf_Email);setFont(jtf_HSL);setFont(jtf_HSPC);setFont(jtf_ChucVu);setFont(jtf_PhongBan);		
		setFont(jbt_CapNhat);setFont(jbt_Huy);
		setFont(jrb_Nam);setFont(jrb_Nu);jrb_Nam.setSelected(true);
		
		jlb_MaNV.setBounds(new Rectangle(20, 50, 120, 25)); 
		jlb_HoTen.setBounds(new Rectangle(20, 80, 120, 25)); 
		jlb_GioiTinh.setBounds(new Rectangle(20, 110, 120, 25)); 
		jlb_SoDT.setBounds(new Rectangle(20, 140, 120, 25)); 
		jlb_Email.setBounds(new Rectangle(20, 170, 120, 25)); 
		jlb_NamSinh.setBounds(new Rectangle(20, 200, 120, 25)); 
		jlb_HSL.setBounds(new Rectangle(20, 230, 120, 25)); 
		jlb_HSPC.setBounds(new Rectangle(20, 260, 120, 25)); 
		jlb_ChucVu.setBounds(new Rectangle(20, 290, 120, 25));
		jlb_PhongBan.setBounds(new Rectangle(20, 320, 120, 25)); 
		ButtonGroup btg = new ButtonGroup(); btg.add(jrb_Nam); btg.add(jrb_Nu);		
		jtf_MaNV.setBounds(new Rectangle(140, 50, 200, 25)); 
		jtf_HoTen.setBounds(new Rectangle(140, 80, 200, 25)); 
		jrb_Nam.setBounds(new Rectangle(140, 110, 60, 25)); 
		jrb_Nu.setBounds(new Rectangle(200, 110, 60, 25)); 
		jtf_SoDT.setBounds(new Rectangle(140, 140, 200, 25)); 
		jtf_Email.setBounds(new Rectangle(140, 170, 200, 25)); 
		jtf_NamSinh.setBounds(new Rectangle(140, 200, 200, 25)); 
		jtf_HSL.setBounds(new Rectangle(140, 230, 200, 25)); 
		jtf_HSPC.setBounds(new Rectangle(140, 260, 200, 25)); 
		jtf_ChucVu.setBounds(new Rectangle(140, 290, 200, 25)); 
		jtf_PhongBan.setBounds(new Rectangle(140, 320, 200, 25)); 
				
		jbt_CapNhat.setBounds(new Rectangle(90, 350, 120, 30)); 
		jbt_Huy.setBounds(new Rectangle(220, 350, 120, 30));
		
		jtf_NamSinh.setHorizontalAlignment(JTextField.RIGHT);
		jtf_HSL.setHorizontalAlignment(JTextField.RIGHT);
		jtf_HSPC.setHorizontalAlignment(JTextField.RIGHT);
	}
	
	public CapNhatNhanVien(){
		setSize(380, 430);
		setVisible(true);
		setTitle("Cập nhật thông tin nhân viên");
		setLayout(null);
		SetLayOut();
		jbt_CapNhat.addActionListener(this);
		jbt_Huy.addActionListener(this);		
		add(jlb_TieuDe);		
		add(jlb_MaNV);add(jlb_HoTen);add(jlb_GioiTinh);add(jlb_SoDT);add(jlb_NamSinh);
		add(jlb_Email);add(jlb_HSL);add(jlb_HSPC);add(jlb_ChucVu);add(jlb_PhongBan);		
		add(jtf_MaNV);add(jtf_HoTen);add(jrb_Nam);add(jrb_Nu);add(jtf_SoDT);add(jtf_NamSinh);
		add(jtf_Email);add(jtf_HSL);add(jtf_HSPC);add(jtf_ChucVu);add(jtf_PhongBan);		
		add(jbt_CapNhat); add(jbt_Huy);
		
		if(index != -1){
			System.out.println(NV.getMaNV());
			jtf_MaNV.setText(NV.getMaNV()); jtf_MaNV.setEnabled(false);
			jtf_HoTen.setText(NV.getHoTen());
			jrb_Nam.setSelected(NV.GioiTinh);
			jtf_SoDT.setText(NV.getSoDT());
			jtf_Email.setText(NV.getEmail());
			jtf_NamSinh.setText(String.valueOf(NV.getNamSinh()));
			jtf_HSL.setText(String.valueOf(NV.getHSL()));
			jtf_HSPC.setText(String.valueOf(NV.getHSPC()));
			jtf_ChucVu.setText(NV.getChucVu());
			jtf_PhongBan.setText(NV.getPhongBan());
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new CapNhatNhanVien();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == jbt_Huy){
			int message = JOptionPane.showConfirmDialog(null, "Bạn có muốn lưu thay đổi không?", "Lưu?", JOptionPane.YES_NO_OPTION);
			if(message == JOptionPane.YES_OPTION){
				SaveList();
				System.exit(0);
			}
			else {
				System.exit(0);
			}
		}
		if(e.getSource() == jbt_CapNhat){
			SaveList();
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
